package com.leesche.zcsp.node.app.v1.controller.update;

import com.leesche.zcsp.node.app.v1.controller.admin.AppAdminController;
import com.leesche.zcsp.node.utils.PropKit;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.model.SysAppInfo;
import com.leesche.zcsp.node.web.service.SysAppInfoService;
import com.leesche.zcsp.node.web.service.impl.data.read.InfoDiscountCountDto;
import com.leesche.zcsp.node.web.service.impl.data.read.PubDiscountCodeDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryAppInfoCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDiscountCodeCondition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dennies yang  on 2016-12-02.
 */
@Controller
@RequestMapping("/app/v1/")
public class AppUpdateController {
    private final static Logger LOGGER = LoggerFactory.getLogger(AppUpdateController.class);

    @Autowired
    private SysAppInfoService sysAppInfoService;

    @ResponseBody
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public Map<String, Object> listView() {
        try {
            Map<String, String> map = new HashMap<String, String>();
           /* String v =PropKit.use("rqcode.properties").get("v");
            String url=PropKit.use("rqcode.properties").get("url");
            String content=PropKit.use("rqcode.properties").get("content");*/
            QueryAppInfoCondition condition = new QueryAppInfoCondition();
            condition.setSuffix("apk");
            SysAppInfo app = sysAppInfoService.findAppByLatestVersion(condition);
            if (app != null) {
                map.put("v", app.getVersion() == null ? "" : app.getVersion());
                map.put("url", app.getReleaseurl() == null ? "" : app.getReleaseurl());
                map.put("content", app.getChangecontent() == null ? "" : app.getChangecontent());
                return WebResult.success().put("data", map).toMap();
            } else {
                return WebResult.failure("已经没有最新版本了").toMap();
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    @RequestMapping(value = "download", method = RequestMethod.GET)
    public String downloadView() {
        try {
            QueryAppInfoCondition condition = new QueryAppInfoCondition();
            condition.setSuffix("apk");
            SysAppInfo app = sysAppInfoService.findAppByLatestVersion(condition);
            if (app == null) {
                return "";
            } else
                return "redirect:" + app.getReleaseurl();
        } catch (Exception e) {
            LOGGER.error(Constants.SYSTEM_ERROR_PROMPT_MSG);
            return "";
        }
    }


}
